package tn.softcom.dfbuilder.modele;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import tn.softcom.dfbuilder.EXCELComponent;
import tn.softcom.dfbuilder.Utils;

public class ADJGCellsGroup {

	private String source;
	private GpsData gpsData;
	private List<ADJGCells> lstADJG;
	private List<Integer> freePosition;
	private List<ADJGCells> lstADJGFailToGetID ;
	private List<RapportADJG> lstRapport;
	public ADJGCellsGroup() {
	}

	public ADJGCellsGroup(String source) {
		super();
		this.source = source;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public List<ADJGCells> getLstADJG() {
		return lstADJG;
	}

	public void setLstADJG(List<ADJGCells> lstADJG) {
		this.lstADJG = lstADJG;
	}

	public void addADJG(ADJGCells ADJG) {
		if (lstADJG == null)
			lstADJG = new ArrayList<ADJGCells>();
		lstADJG.add(ADJG);
	}
	
	public void addRapport(RapportADJG rapport) {
		if (lstRapport == null)
			lstRapport = new ArrayList<RapportADJG>();
		lstRapport.add(rapport);
	}
	
	public void addADJGFail(ADJGCells ADJG) {
		if (lstADJGFailToGetID == null)
			lstADJGFailToGetID = new ArrayList<ADJGCells>();
		lstADJGFailToGetID.add(ADJG);
	}

	private void setFreePosition() {
		try {
			Connection conn = Utils.getConnection();
			Statement s = conn.createStatement();

			
			String queryCount = "SELECT count(A_ADJG.ADJGId) FROM A_WCEL INNER JOIN A_ADJG ON (A_WCEL.LcrId = A_ADJG.LcrId) AND (A_WCEL.WBTSId = A_ADJG.WBTSId) AND (A_WCEL.RncId = A_ADJG.RncId) WHERE A_WCEL.CellAdditionalInfo = '"+source+"'";
			s.execute(queryCount);
			ResultSet rsqueryCount = s.getResultSet();
			int count = 0;;
			if ((rsqueryCount != null) && (rsqueryCount.next())) {
				count = rsqueryCount.getInt(1);
			}
			// create a table
			String query = "SELECT A_ADJG.ADJGId FROM A_WCEL INNER JOIN A_ADJG ON (A_WCEL.LcrId = A_ADJG.LcrId) AND (A_WCEL.WBTSId = A_ADJG.WBTSId) AND (A_WCEL.RncId = A_ADJG.RncId) WHERE A_WCEL.CellAdditionalInfo = '"+source+"'";
			s.execute(query);
			ResultSet rs = s.getResultSet();
			freePosition = new ArrayList<Integer>();
			int free = 32 - count;
			int tab [] = new int[32];
			if(free > 0){
				while ((rs != null) && (rs.next())) {
					int id = Integer.valueOf(rs.getString("ADJGId")) ;
					tab [id] = id;
				}
				for(int i = 0;i<32; i++){
					if(tab[i] == 0)
						freePosition.add(i);
				}
			}
			
			// close and cleanup
			s.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void setADJGID(){
		setFreePosition() ;
		if(freePosition!=null && freePosition.size()>0){
			if(freePosition.size()>=lstADJG.size()){
				int i = 0;
				for(ADJGCells ADJG:lstADJG){
					ADJG.setADJGId(""+freePosition.get(i));
					i++;
				}
			}else{
				for(int i =0;i<freePosition.size();i++){
					lstADJG.get(i).setADJGId(""+freePosition.get(i));
				}
				
				for(int i = freePosition.size();i<lstADJG.size();i++){
					addADJGFail((ADJGCells) lstADJG.get(i).clone());
					lstADJG.get(i).setADJGId("MAX");
				}
			}
		}else{
			for(int i = 0;i<lstADJG.size();i++){
				addADJGFail((ADJGCells) lstADJG.get(i).clone());
				lstADJG.get(i).setADJGId("MAX");
			}
		}
		
	}
	
	
	public RapportADJG retreiveNewId(){
		String id = "";
		RapportADJG rt = new RapportADJG();
		while(lstRapport != null && lstRapport.size()>0 && "".equals(id)){
			Collections.sort(lstRapport);
			System.out.println("rapport size = " +lstRapport);
			RapportADJG rapport = lstRapport.get(0);
			try {
				Connection conn = Utils.getConnection();
				Statement s = conn.createStatement();
				String query = "SELECT A_ADJG.ADJGId FROM A_WCEL INNER JOIN A_ADJG ON (A_WCEL.LcrId = A_ADJG.LcrId) AND (A_WCEL.WBTSId = A_ADJG.WBTSId) AND (A_WCEL.RncId = A_ADJG.RncId) where A_WCEL.CellAdditionalInfo = '"+rapport.getSource_WCEL_Name()+"' and A_ADJG.name = '"+rapport.getTarget_Segment_Name()+"' and A_WCEL.AdminCellState =1";
				s.execute(query);
				ResultSet rs = s.getResultSet();
				if(rs != null && (rs.next())) {
					 id = rs.getString("ADJGId") ;
					 rt = (RapportADJG) rapport.clone();
					 rt.setIdToRetreve(id);
				}
				
				s.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			lstRapport.remove(0);
		}
		return rt;
	}
	

	public List<ADJGCells> getLstADJGFailToGetID() {
		return lstADJGFailToGetID;
	}

	public List<RapportADJG> getLstRapport() {
		return lstRapport;
	}


	public GpsData getGpsData() {
		return gpsData;
	}

	public void setGpsData(GpsData gpsData) {
		this.gpsData = gpsData;
	}
}
